package dieciLuglio;


import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.*;

public class ClockPanel extends JPanel{
	private JLabel ora=new JLabel("    Ora: ");
	private JLabel clockLabel;
	private DateFormat dateFormat;
	private Timer timer;
	private int second = -1;
	private JLabel data;

	public ClockPanel (){
		setLayout(new FlowLayout());

		dateFormat = DateFormat.getTimeInstance ();

		clockLabel = new JLabel ();
		data=new JLabel();
		add(data);
		add(ora);
		add (clockLabel);

		timer = new Timer (1000, new ActionListener (){
			public void actionPerformed (ActionEvent e){
				Calendar c = Calendar.getInstance ();
				GregorianCalendar gc=new GregorianCalendar();
				String giorno=String.valueOf(gc.get(GregorianCalendar.DAY_OF_MONTH));
				String mese=String.valueOf(gc.get(GregorianCalendar.MONTH)+1);
				String anno=String.valueOf(gc.get(GregorianCalendar.YEAR));
				data.setText("Data: "+giorno+"/"+mese+"/"+anno);

				if (c.get (Calendar.SECOND) != second){
					String str = dateFormat.format (c.getTime ());

					clockLabel.setText (str);

					second = c.get (Calendar.SECOND);
				}
			}
		});

		timer.start ();
	}
}